package org.jeecg.modules.system.util;


import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.util.Random;

public class AccountGenerator {

    // 拼音输出格式配置
    private static final HanyuPinyinOutputFormat pinyinFormat = new HanyuPinyinOutputFormat();
    static {
        pinyinFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        pinyinFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    }

    // 生成账号主方法
    public static String generateAccount(String province, String city, String county, String town, String village) {
        // 检查必要字段
        if (city == null || county == null || town == null || village == null) {
            return "";
        }
        
        // 处理行政区域名称缩写
        String cityAbbr = getAbbreviation(removeSuffix(city, "市", "地区"));
        String countyAbbr = getAbbreviation(removeSuffix(county, "县", "区", "市"));
        String townAbbr = getAbbreviation(removeSuffix(town, "镇", "乡"));
        String villageAbbr = getAbbreviation(village);
        
        // 生成四位随机数
        String randomSequence = generateRandomSequence();
        
        // 拼接完整账号
        return cityAbbr + countyAbbr + townAbbr + villageAbbr + randomSequence;
    }

    // 去除行政区划后缀
    private static String removeSuffix(String name, String... suffixes) {
        if (name == null) return "";
        for (String suffix : suffixes) {
            if (name.endsWith(suffix)) {
                return name.substring(0, name.length() - suffix.length());
            }
        }
        return name;
    }

    // 获取中文缩写（拼音首字母）
    private static String getAbbreviation(String text) {
        if (text == null || text.isEmpty()) return "";
        
        StringBuilder sb = new StringBuilder();
        for (char ch : text.toCharArray()) {
            if (Character.toString(ch).matches("[\\u4E00-\\u9FA5]")) {
                try {
                    String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(ch, pinyinFormat);
                    if (pinyinArray != null && pinyinArray.length > 0) {
                        sb.append(pinyinArray[0].charAt(0));
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    // 忽略无法转换的字符
                }
            } else if (Character.isLetter(ch)) {
                sb.append(Character.toUpperCase(ch));
            }
        }
        return sb.toString();
    }

    // 生成4位随机序列号
    private static String generateRandomSequence() {
        Random random = new Random();
        return String.format("%04d", random.nextInt(10000));
    }

    // Excel数据处理示例
//    public static void main(String[] args) {
//        // 模拟Excel行数据
//        String[] rowData = {
//            "江西省", "赣州市", "石城县", "琴江镇", "桐坪村", "第五小组"
//        };
//
//        // 生成账号
//        String account = generateAccount(
//            rowData[0], // 省
//            rowData[1], // 市
//            rowData[2], // 区县
//            rowData[3], // 乡镇
//            rowData[4]  // 村
//        );
//
//        System.out.println("生成的账号: " + account);
//        // 示例输出: GZSCQJTP5839 (赣州+石城+琴江+桐坪+随机数)
//    }
}